<template>
    <div class="container-pass">
        <el-card class="pass-card">
            <el-card class="display-pass" v-for="item in passwords" :key="item.id">
                <div class="clearfix">
                    <div>
                         <el-input placeholder="请输入内容" v-model="item.passName">
                            <template slot="prepend">{{item.conName}}的账号</template>
                        </el-input>
                    </div>
                    <div>
                         <el-input placeholder="请输入内容" v-model="item.password">
                            <template slot="prepend">{{item.conName}}的密码</template>
                        </el-input>
                    </div>
                    <div>
                        <el-button type="danger" size="mini" round @click="deletePass(item.id)">删除</el-button>
                        <el-button type="primary" size="mini" round @click="editPassDialog(item.id)">编辑</el-button>
                    </div>
                </div>
            </el-card>
            <el-card class="add">
                <el-button type="primary" @click="dialogVisible = true">添加</el-button>
            </el-card>
        </el-card>

        <el-dialog
          title="添加账号"
          :visible.sync="dialogVisible"
          width="500px"
          @closed="dialogClose">
          <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
            <el-form-item label="账号" prop="passName">
              <el-input v-model="addForm.passName" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="addForm.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="公司" prop="company">
              <el-input v-model="addForm.company" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addPass">确 定</el-button>
          </span>
        </el-dialog>

        <el-dialog
          title="修改账号"
          :visible.sync="editDialogVisible"
          width="500px"
          @closed="dialogClose">
          <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
            <el-form-item label="账号" prop="passName">
              <el-input v-model="addForm.passName" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="addForm.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="公司" prop="company">
              <el-input v-model="addForm.company" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="updatePass">确 定</el-button>
          </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
  data() {
    return {
      addForm: {
        passName: '',
        password: '',
        company: '',
        id: ''
      },
      tip: 'qq密码',
      passwords: [],
      dialogVisible: false,
      addFormRules: {
        passName: [{ required: true, message: '请填写账号', trigger: 'blur' }],
        password: [{ required: true, message: '请填写密码', trigger: 'blur' }],
        company: [{ required: true, message: '请填写公司名称', trigger: 'blur' }]
      },
      editDialogVisible: false
    }
  },
  created() {
    // 获得当前用户的密码数据
    this.getPasswords()
  },
  methods: {
    async getPasswords() {
      const { data: res } = await this.$http.get('/pm/getPass')
      if (res.meta.status !== 200) {
        return this.$message.error('获取账号失败或没有账号')
      }
      this.passwords = res.data
    },
    addPass() {
      this.$refs.addFormRef.validate(
        async (valid) => {
          if (!valid) return
          const { data: res } = await this.$http.post('/pm/addPass', this.addForm)
          if (res.meta.status !== 200) {
            return this.$message.error('添加账号失败')
          }
          this.$message.success('添加账号成功')
          this.dialogVisible = false
          this.getPasswords()
        }
      )
    },
    // 监听对话框关闭
    dialogClose() {
      // 重置表单
      this.$refs.addFormRef.resetFields()
      this.addForm.passName = ''
      this.addForm.company = ''
      this.addForm.password = ''
      this.addForm.id = ''
    },
    // 监听修改对话框关闭
    editDialogVisibleClosed() {
      this.$refs.addFormRef.resetFields()
      this.addForm.passName = ''
      this.addForm.company = ''
      this.addForm.password = ''
      this.addForm.id = ''
    },
    // 删除账号
    async deletePass(id) {
      // 弹框询问是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该计划,是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)

      // 如果用户确认删除,则返回字符串confirm
      // 如果用户取消删除,则返回字符串cancel
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete('/pm/deletePass/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败！')
      }
      this.$message.success('删除成功')
      this.getPasswords()
    },
    // 修改账号对话框
    async editPassDialog(id) {
      // 首先根据id查询对应的当个账号然后复制给addForm
      const { data: res } = await this.$http.get('/pm/getOne/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('获取账号失败')
      }
      this.addForm.passName = res.data.passName
      this.addForm.password = res.data.password
      this.addForm.company = res.data.conName
      this.addForm.id = res.data.id
      // 展示对话框
      this.editDialogVisible = true
    },
    // 发送请求修改账号事件
    updatePass() {
      // 先进行表单验证
      this.$refs.addFormRef.validate(
        async (valid) => {
          if (!valid) return
          const { data: res } = await this.$http.post('/pm/update', this.addForm)
          if (res.meta.status !== 200) {
            return this.$message.error('修改失败!')
          }
          // 关闭对话框
          this.editDialogVisible = false
          // 重新获取一遍列表
          this.getPasswords()
        }
      )
    }
  }
}
</script>

<style lang="less" scoped>

.container-pass {
    padding: 20px;
    background-color: #909399;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.pass-card {
    width: 1000px;
    height: 900px;
    overflow: auto;
    background-color: #DCDFE6;
}

el-card__body {
    padding: 2px;
}

.clearfix{
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.display-pass {
    margin-bottom: 20px;
}

.add {
    text-align: center;
}
</style>
